﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_Contact : System.Web.UI.UserControl
{
    private Logging objLogging = new Logging();
    private string strApplicationName = MethodBase.GetCurrentMethod().DeclaringType.Name;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {

            if (!string.IsNullOrEmpty(PhoneHome.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
        catch (Exception ex)
        {
            objLogging.WriteLine(strApplicationName, ex.Message);
        }
    }
    protected void SendButton_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string fileName = Server.MapPath("~/User/ContactForm.txt");
            string mailBody = File.ReadAllText(fileName);

            mailBody = mailBody.Replace("##Name##", Name.Text);
            mailBody = mailBody.Replace("##Email##", EmailAddress.Text);
            mailBody = mailBody.Replace("##HomePhone##", PhoneHome.Text);
            mailBody = mailBody.Replace("##Comments##", Comments.Text);

            try
            {
                MailMessage myMessage = new MailMessage();
                myMessage.Subject = "Response from web site";
                myMessage.Body = mailBody;

                myMessage.From = new MailAddress(AppConfiguration.FromAddress, AppConfiguration.FromName);
                myMessage.To.Add(new MailAddress(AppConfiguration.ToAddress, AppConfiguration.ToName));

                SmtpClient mySmtpClient = new SmtpClient();
                mySmtpClient.Send(myMessage);

                MessageSentPara.Visible = true;
                FormTable.Visible = false;
                System.Threading.Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                Message.Text = "An error occurred while sending your e-mail. Please try again.";
                objLogging.WriteLine(strApplicationName, ex.Message);
            }
            finally
            {
                Message.Visible = true;
            }

        }
    }
}